package com.lightheart.sphr.xiaobinggan.design


import android.graphics.Color
import android.support.design.widget.AppBarLayout
import android.support.design.widget.TabLayout
import com.lightheart.sphr.xiaobinggan.R
import com.lightheart.sphr.xiaobinggan.base.BaseActivity
import com.lightheart.sphr.xiaobinggan.design.adapter.Adapter_Page
import com.lightheart.sphr.xiaobinggan.fragment.FragmentOne
import com.lightheart.sphr.xiaobinggan.fragment.FragmentThree
import com.lightheart.sphr.xiaobinggan.fragment.FragmentTwo
import com.lightheart.sphr.xiaobinggan.module.home.presenter.HomePresenter
import com.lightheart.sphr.xiaobinggan.utils.AppBarStateChangeListener
import kotlinx.android.synthetic.main.activity_materia_design.*
import kotlinx.android.synthetic.main.activity_materia_design.tabs
import kotlinx.android.synthetic.main.activity_materia_design.view_pager
import kotlinx.android.synthetic.main.activity_top_app_bar.*


class TopAppBarActivity : BaseActivity<HomePresenter>() {

    override fun getLayoutId() = R.layout.activity_top_app_bar

    override fun initInjector() {
    }

    override fun initView() {
        initViewPager()
        appBarLayout.addOnOffsetChangedListener(object : AppBarStateChangeListener() {
            override fun onStateChanged(appBarLayout: AppBarLayout?, state: State) {
                if (state === State.EXPANDED) {
                    //展开状态
                    common_toolbar.setBackgroundColor(Color.parseColor("#00000000"))
                } else if (state === State.COLLAPSED) {
                    //折叠状态
                    common_toolbar.setBackgroundColor(Color.parseColor("#C01E2F"))
                } else {
                    //中间状态
                    common_toolbar.setBackgroundColor(Color.parseColor("#00000000"))
                }
            }
        })

        tv.setOnClickListener {
            appbar.getTotalScrollRange()
        }

    }

    fun initViewPager() {
        val adapter = Adapter_Page(supportFragmentManager)
        adapter.addFragment(FragmentOne(), "主页")
        adapter.addFragment(FragmentTwo(), "日记")
        adapter.addFragment(FragmentThree(), "相册")
        view_pager.adapter = adapter
        //设置tablayout，viewpager上的标题
        tabs.setupWithViewPager(view_pager)
        tabs.setSelectedTabIndicatorColor(resources.getColor(R.color.colorPrimary))
        view_pager.currentItem = 1
        tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
            }

        })
    }


}